#include "JBaseFile.h"
#include <boost/filesystem.hpp>
#include "JDir.h"
#include "JFileException.h"
#include "JException.h"
using namespace boost::filesystem;

JBaseFile::JBaseFile(void)
{

}

JBaseFile::JBaseFile( std::string _fileName,std::string _createTime,std::string _verifyTime ) : filename(_fileName),createTime(_createTime),verifyTime(_verifyTime)
{

}

JBaseFile::JBaseFile( std::string _fileName ):filename(_fileName)
{

}


JBaseFile::~JBaseFile(void)
{
}





int JBaseFile::Serialize( std::iostream& _strea,int& allSize )
{
	////Serialize begin.
	//std::string filename;
	//std::string createTime;
	//std::string verifyTime;
	////Serialize end.
	int classSize = 0,selfSize = 0;
	JSerialize::PutInt(_strea,classSize,selfSize);
	JSerialize::Serialize(_strea,filename,classSize);
	JSerialize::Serialize(_strea,createTime,classSize);
	JSerialize::Serialize(_strea,verifyTime,classSize);
	selfSize += classSize;
	JSerialize::Verify(_strea,classSize,-1*selfSize);
	allSize += selfSize;
	return selfSize;
}

void JBaseFile::Reserialize( std::iostream& _strea )
{
	int classSize = JSerialize::GetInt(_strea,sizeof(int));
	this->filename = JSerialize::ReSerializeString(_strea);
	this->createTime = JSerialize::ReSerializeString(_strea);
	this->verifyTime = JSerialize::ReSerializeString(_strea);
}


std::string JBaseFile::Absolute( std::string _fileName )
{
	path p = absolute(_fileName);
	return p.string();
}

bool JBaseFile::HasGenerateFile( std::string ancestor,std::string _fileName )
{
	if((_fileName.find(ancestor) != std::string::npos || _fileName == ancestor) && exists(ancestor))
	{
		return true;
	}
	return false;
}

std::list<std::string> JBaseFile::RecurseDownBaseFile( std::string _rootPath )
{
	std::list<std::string> listFileName;
	if(exists(_rootPath))
	{
		if(is_directory(_rootPath))
		{
			listFileName = JDir::RecurseDownDir(_rootPath);
		}
		else
		{
			listFileName.push_back(_rootPath);
		}
	}
	return listFileName;
}

void JBaseFile::ReName( std::string oldFileName,std::string newFileName )
{
	try
	{
		path p(oldFileName);
		path p2(newFileName);
		rename(p,p2);
	}
	catch(filesystem_error& e)
	{
            throw JFileException(e.what());
	}
	catch(std::exception e)
	{
            throw JFileException(e.what());
	}

}

bool JBaseFile::HasChildBaseFile( std::string parentPath,std::string baseFileName )
{
	if(is_directory(parentPath))
	{
		directory_iterator ite = directory_iterator(parentPath);
		for(;ite != directory_iterator(); ++ite)
		{
			if(ite->path().string() == baseFileName)
			{
				return true;
			}
		}
	}
	return false;
}

bool JBaseFile::IsExist( std::string _path )
{
	if(exists(_path))
	{
		return true;
	}
	return false;
}

bool JBaseFile::CopyBaseFile( std::string oldFileName,std::string newFileName )
{
	//boost::system::error_code ec;
	try
	{
		copy(oldFileName,newFileName);
	}
	catch(boost::exception& e)
	{
		return false;
	}
	return true;
}
